/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.submit;

import java.util.Calendar;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import net.bioteam.appweb.Config;
import net.bioteam.appweb.gwt.apps.clientandserver.Constant;
import net.bioteam.common.user.User;
import net.bioteam.common.user.UserInfo;
import net.bioteam.common.utils.MyEntityManager;

public class SubmitterUtil
{

	public static CommandSubmitter getSubmitter(Long clusterid)
	{

		if (clusterid == RunStatus.LOCAL_CLUSTER)
		{
			return Config.submitter;
		} else
		{

			return new SoapCommandSubmitter();
		}
	}

	// called by web or ws
	public static UnfinishedJob submitJob(SubmitData data) throws Exception
	{

		String clustervalue = data.getBean().getValue(Constant.progranm_form_cluster_name);
		Long clusterid = null;
		if (clustervalue != null)
		{
			clusterid = Long.parseLong(clustervalue);
		} else
		{
			clusterid = RunStatus.LOCAL_CLUSTER;
			data.getBean().setValue(Constant.progranm_form_cluster_name, String.valueOf(RunStatus.LOCAL_CLUSTER));
		}

		if (clusterid == RunStatus.LOCAL_CLUSTER)
		{
			User user = data.getUser();
			if (user.getUserInfo().getDailyquote() != UserInfo.UNLIMITEDQUOTE)
			{
				EntityManager em = MyEntityManager.getEntityManager();
				Query totalQuery = em.createNamedQuery("jobfromtime");
				Calendar nowc = Calendar.getInstance();
				nowc.set(Calendar.SECOND, 0);
				nowc.set(Calendar.MINUTE, 0);
				nowc.set(Calendar.HOUR, 0);
				nowc.set(Calendar.MILLISECOND, 0);
				nowc.set(Calendar.AM_PM, Calendar.AM);
				totalQuery.setParameter("presubmittime", nowc.getTime());
				totalQuery.setParameter("username", user.getIdAsString());
				try
				{
					Number result = (Number) totalQuery.getSingleResult();
					int count = result.intValue();
					if (count >= user.getUserInfo().getDailyquote())
						throw new Exception("You have reached your daily quote");
				} catch (javax.persistence.NoResultException e)
				{

				}
			}
		}
		CommandSubmitter submitter = getSubmitter(clusterid);
		UnfinishedJob job = submitter.saveJobInfo(data);
		if (!job.isRemoteJob())
		{
			return submitter.submitCommand(job);
		} else
		{
			return job;
		}
	}

	/**
	 * called by job collector
	 */
	public static UnfinishedJob submitJob(UnfinishedJob job) throws Exception
	{

		CommandSubmitter submitter = getSubmitter(job.getClusterid());
		if (submitter.canSubmit(job))
		{
			return submitter.submitCommand(job);
		} else
		{
			return job;
		}
	}

}
